//
//  ViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/10/18.
//

#import "ViewController.h"
#import "BaseTabbarViewController.h"
#import "BaseNavgationViewController.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "NextViewController.h"
#import "SaUserInfoCenter.h"
#import "SaUserDataCenter.h"
#import <SangforSDK/SFUemSDK.h>
#import "HomeZJLoginPublicApiManager.h"
#import "ChangePasswordViewController.h"
#import "UserAgreementViewController.h"
#import "UserPrivacyAgreementViewController.h"

#import "PushManager.h"


@interface ViewController ()<SFAuthResultDelegate,protocolChangeXieyiTypeDelegate>

//@property (nonatomic,strong) UIImageView * follewImg;
//@property (nonatomic,strong) UIImageView * iconImg;
//@property (nonatomic,strong) UILabel * iconLabel;
//@property (nonatomic,strong) UILabel * twoLabel;

@property (nonatomic,strong) UIView * titleFollewView;
@property (nonatomic,strong) UIImageView * img;

@property (nonatomic,strong) UILabel * titileLabel;
@property (nonatomic,strong) UILabel * msgLabel;
@property (nonatomic,strong) UILabel * msgTwoLabel;



@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) UIImageView * userNameImg;
@property (nonatomic,strong) UILabel * userNameLabel;
@property (nonatomic,strong) UITextField * userNameTextF;
@property (nonatomic,strong) UIView * userNameV;

@property (nonatomic,strong) UIImageView * passwordImg;
@property (nonatomic,strong) UILabel * passwordLabel;
@property (nonatomic,strong) UIButton * forgetPassWordBtn;
@property (nonatomic,strong) UITextField * passwordTextF;
@property (nonatomic,strong) UIView * passwordV;

@property (nonatomic,strong) UIButton * gxBtn;
@property (nonatomic,strong) UILabel * xieyiLabel;
@property (nonatomic,assign) BOOL xieyiBool;


@property (nonatomic,strong) UIButton * nextBtn;

@property (nonatomic,strong) HomeZJLoginPublicApiManager * manager;

@property (nonatomic,strong) NextViewController *  nextViewController;


@end

@implementation ViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.xieyiBool = NO;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [[SFUemSDK sharedInstance] setAuthResultDelegate:self];
    NSLog(@"getAuthStatus   %ld",(long)[[SFUemSDK sharedInstance].auth getAuthStatus]);
    
    [self setUpUI];


}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}





-(void)setUpUI{
    
//    [self.view addSubview:self.follewImg];
//    [self.follewImg addSubview:self.iconImg];
//    [self.follewImg addSubview:self.iconLabel];
//    [self.follewImg addSubview:self.twoLabel];
    
    [self.view addSubview:self.titleFollewView];
    [self.titleFollewView addSubview:self.titileLabel];
    [self.titleFollewView addSubview:self.img];
    [self.titleFollewView addSubview:self.msgLabel];
    [self.titleFollewView addSubview:self.msgTwoLabel];
    
    
    [self.view addSubview:self.follewView];
    [self.follewView addSubview:self.userNameImg];
    [self.follewView addSubview:self.userNameLabel];
    [self.follewView addSubview:self.userNameTextF];
    [self.follewView addSubview:self.userNameV];
    
    [self.follewView addSubview:self.passwordImg];
    [self.follewView addSubview:self.passwordLabel];
    [self.follewView addSubview:self.forgetPassWordBtn];
    [self.follewView addSubview:self.passwordTextF];
    [self.follewView addSubview:self.passwordV];
    
    [self.follewView addSubview:self.gxBtn];
    [self.follewView addSubview:self.xieyiLabel];
    
    [self.follewView addSubview:self.nextBtn];
    
    
//    [self.follewImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.equalTo(self.view);
//        make.height.equalTo(@(250));
//    }];
//
//    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.follewImg).with.offset(ALIGN_WIDTH_PT(20));
//        make.centerY.equalTo(self.follewImg.mas_centerY);
//        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(64), ALIGN_WIDTH_PT(64))));
//    }];
//    [self.iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.iconImg.mas_right).with.offset(ALIGN_WIDTH_PT(10));
//        make.top.equalTo(self.iconImg.mas_top).offset(ALIGN_HEIGHT_PT(10));
//    }];
//
//    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.iconImg.mas_right).with.offset(ALIGN_WIDTH_PT(10));
//        make.top.equalTo(self.iconLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(5));
//    }];
    
    [self.titileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset([[UIApplication sharedApplication]statusBarFrame].size.height);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.titleFollewView);
        make.size.equalTo(@(CGSizeMake(ALIGN_HEIGHT_PT(214), ALIGN_HEIGHT_PT(214))));
    }];
    
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleFollewView).with.offset(ALIGN_WIDTH_PT(14));
        make.top.equalTo(self.titileLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(32));
    }];
    
    [self.msgTwoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleFollewView).with.offset(ALIGN_WIDTH_PT(14));
        make.top.equalTo(self.msgLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(5));
    }];
    
    
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.titleFollewView.mas_bottom).with.offset(-20);
    }];
    [self.userNameImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(20));
        make.top.equalTo(self.follewView).with.offset(ALIGN_HEIGHT_PT(30));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(20), ALIGN_WIDTH_PT(20))));
    }];
    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userNameImg.mas_right).with.offset(ALIGN_WIDTH_PT(10));
        make.centerY.equalTo(self.userNameImg.mas_centerY);
    }];
    
    [self.forgetPassWordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(20));
        make.centerY.equalTo(self.passwordLabel.mas_centerY);
    }];
    
    [self.userNameTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userNameImg.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(20));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(20));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(40)));
    }];
    [self.userNameV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(20));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(20));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(1)));
        make.top.equalTo(self.userNameTextF.mas_bottom);
    }];
    
    [self.passwordImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userNameImg);
        make.top.equalTo(self.userNameV.mas_bottom).with.offset(ALIGN_HEIGHT_PT(30));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(20), ALIGN_WIDTH_PT(20))));
    }];

    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userNameLabel);
        make.centerY.equalTo(self.passwordImg.mas_centerY);
    }];
    [self.passwordTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(20));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(20));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(40)));
        make.top.equalTo(self.passwordImg.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    [self.passwordV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(20));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(20));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(1)));
        make.top.equalTo(self.passwordTextF.mas_bottom);
    }];
    
    
    [self.gxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordTextF.mas_left);
        make.top.equalTo(self.passwordV.mas_bottom).with.offset(ALIGN_HEIGHT_PT(20));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(14), ALIGN_WIDTH_PT(14))));
    }];
    
    [self.xieyiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.gxBtn.mas_centerY);
        make.left.equalTo(self.gxBtn.mas_right).with.offset(ALIGN_WIDTH_PT(5));
    }];
    
    
    
    
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(12));
        make.bottom.equalTo(self.follewView.mas_bottom).with.offset(-ALIGN_HEIGHT_PT(40));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
    }];
    
    
    
}

#pragma mark -- 点击事件
-(void)respondsTo_forgetPasswordBtn{ // 忘记密码
    

//    NextViewController * vc = [NextViewController new];
//    [self.navigationController pushViewController:vc animated:YES];
    
//    AppDelegate * Appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [Appdelegate ToTabbar];
    
    
    NSMutableDictionary * dic = @{}.mutableCopy;
    [dic setValue:self.userNameTextF.text forKey:@"username"];
    [dic setValue:self.passwordTextF.text forKey:@"password"];
    [dic setValue:@"APP" forKey:@"clientId"];
    [dic setValue:@"admin123" forKey:@"clientSecret"];
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"正在加载...";
    [hud removeFromSuperViewOnHide];
    
    [self.manager dispatchTaskWithParam:dic completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        [hud hideAnimated:YES];
        
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
            // 请求成功 保存数据
            SaUserInfoCenter *info = [SaUserInfoCenter mj_objectWithKeyValues:responseObject[@"data"]];
            NSLog(@"%@ ==== %@", [SaUserInfoCenter shareInstance], info);
            [[SaUserInfoCenter shareInstance] configData:info];
//            7a019ab4eae163a508fec15f65abd13e0242112b21086fdd49f6f917dfb6ddd2
            
//            NSString *token = [self getDeviceTokenString:deviceToken];
            NSUserDefaults *defaultsToken = [NSUserDefaults standardUserDefaults];
//            [defaults setObject:token forKey:@"deviceToken"];
//            NSLog(@"Device Token: %@", token);
            
            // TODO: 将token发送给服务器
            NSString * token = [defaultsToken objectForKey:@"deviceToken"];
            if(token) {
//                NSLog(@"========%@",@([SaUserDataCenter shareIntance].isLogin));
//                [[HmsInstanceId getInstance] getToken:token subjectId:[NSString stringWithFormat:@"%ld",(long)info.userId]];
            
            }
            
    
            
            /* FMDB有错 空了可以打开看下sql语句 */
//            SaUserDataCenter * userData = [SaUserDataCenter shareIntance];
//            userData.accessToken = info.accessToken;
//            userData.userId = info.userId;
//            userData.expiresTime = info.expiresTime;
//            userData.refreshToken = info.refreshToken;
//            userData.isLogin = YES;
//            if ([userData save]) {
//
//                AppDelegate * Appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//                [Appdelegate ToTabbar];
//            }
            
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:info.accessToken forKey:@"accessToken"];
            [defaults setObject:info.refreshToken forKey:@"refreshToken"];
            [defaults setObject:info.expiresTime forKey:@"expiresTime"];
            [defaults setObject:@(info.userId) forKey:@"userId"];
            [defaults setObject:@(1) forKey:@"isLogin"];
            AppDelegate * Appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [Appdelegate ToTabbar];
            
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }

    }];
    
    
}

-(void)togglePasswordVisibility:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.passwordTextF.secureTextEntry = !sender.selected;
}

-(void)respondsto_ChangeXieyiType:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.gxBtn setImage:[UIImage imageNamed:@"勾"] forState:(UIControlStateSelected)];
        self.xieyiBool = YES;
    }else{
        [self.gxBtn setImage:[UIImage imageNamed:@"未选中圆圈"] forState:(UIControlStateNormal)];
        self.xieyiBool = NO;
    }
}



-(void)changeXieyiType{
    [self.gxBtn setImage:[UIImage imageNamed:@"勾"] forState:(UIControlStateSelected)];
    self.xieyiBool = YES;
}


-(void)respondsTo_pushNextViewController:(UIButton *)sender{ // 下一步
    
//    ChangePasswordViewController * vc = [ChangePasswordViewController new];
////        vc.phoneStr = self.userNameTextF.text;
//    vc.phoneText = self.userNameTextF.text;
////        vc.passwordStr = self.passwordTextF.text;
//    [self.navigationController pushViewController:vc animated:YES];
    if (self.xieyiBool == NO){
        [MBProgressHUD showError:@"请勾选或者阅读协议"];
        return;
    }
    
    if (self.userNameTextF.text.length < 11 || self.userNameTextF.text.length == 0){
        [MBProgressHUD showError:@"请输入正确账号"];
    }else if (self.passwordTextF.text.length == 0){
        [MBProgressHUD showError:@"请输入密码"];
    }else{
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [[SFUemSDK sharedInstance] startPasswordAuth:[NSURL URLWithString:@"https://kzqtxzvpn.caacsri.com:9998"] userName:self.userNameTextF.text password:self.passwordTextF.text];
    //
    //    NextViewController * vc = [NextViewController new];
    //    vc.phoneStr = self.userNameTextF.text;
    //    vc.passwordStr = self.passwordTextF.text;
    //    [self.navigationController pushViewController:vc animated:YES];
    }
    
}






#pragma mark -- SFAuthResultDelegate

- (void)onAuthFailed:(SFBaseMessage *)message {
    [MBProgressHUD hideHUDForView:self.view];
    [LUtilityTools showAlertWithTitle:@"温馨提示" message:message.errStr buttonAction:^{
        
    }];
}
- (void)onAuthSuccess:(SFBaseMessage *)message {
    [MBProgressHUD hideHUDForView:self.view];
    [self saveLoginInfo];
    [self respondsTo_forgetPasswordBtn];
//    [[SFUemSDK sharedInstance] g];

}



- (void)saveLoginInfo {
//    NSString *url = self.pAddrTextField.text;
    NSString *username = self.userNameTextF.text;
//    NSString *password = self.passwordLabel.text;
    
//    if (url.length == 0) {
//        return;
//    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setObject:url forKey:@"url"];
    [defaults setObject:username forKey:@"username"];
//    [defaults setObject:password forKey:@"password"];
    
    [defaults synchronize];
}



- (void)onAuthProcess:(SFAuthType)nextAuthType message:(SFBaseMessage *)message {
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MBProgressHUD hideHUDForView:self.view];
    if (nextAuthType == SFAuthTypeSMS) {
        self.nextViewController = [NextViewController new];
        self.nextViewController.phoneStr = self.userNameTextF.text;
        self.nextViewController.passwordStr = self.passwordTextF.text;
        [self.navigationController pushViewController:self.nextViewController  animated:YES];
        
    }    if (nextAuthType == SFAuthTypeRenewPassword) {
        if (self.nextViewController ) {
            [self.nextViewController.navigationController popViewControllerAnimated:false];
        }
        ChangePasswordViewController * vc = [ChangePasswordViewController new];
//        vc.phoneStr = self.userNameTextF.text;
//        vc.phoneText = self.userNameTextF.text;
//        vc.passwordStr = self.passwordTextF.text;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    
}



#pragma mark -- 懒加载
//-(UIImageView *)follewImg{
//    if (!_follewImg) {
//        _follewImg = [UIImageView new];
//        _follewImg.image = [UIImage imageNamed:@"Mask group-1"];
//    }
//    return _follewImg;
//}
//
//-(UIImageView *)iconImg{
//    if (!_iconImg) {
//        _iconImg = [UIImageView new];
//        _iconImg.image = [UIImage imageNamed:@"机场logo 2"];
//    }
//    return _iconImg;
//}
//
//-(UILabel *)iconLabel{
//    if (!_iconLabel) {
//        _iconLabel = [UILabel new];
//        _iconLabel.text = @"欢迎使用";
//        _iconLabel.font = FONT18;
//        _iconLabel.textColor = [UIColor blackColor];
//        _iconLabel.textAlignment = NSTextAlignmentLeft;
//    }
//    return _iconLabel;
//}
//
//-(UILabel *)twoLabel{
//    if (!_twoLabel) {
//        _twoLabel = [UILabel new];
//        _twoLabel.text = @"机场数字通行证系统";
//        _twoLabel.font = FONT16;
//        _twoLabel.textColor = COLOR_RGB(50, 113, 237);
//        _twoLabel.textAlignment = NSTextAlignmentLeft;
//    }
//    return _twoLabel;
//}



-(UIView *)titleFollewView{
    if (!_titleFollewView) {
        _titleFollewView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ALIGN_HEIGHT_PT(260))];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,SCREEN_WIDTH,260);
        gl.startPoint = CGPointMake(0.76, 0.5);
        gl.endPoint = CGPointMake(0.24, 0.5);
        gl.colors = @[(__bridge id)COLOR_RGB(102, 151, 246).CGColor,(__bridge id)COLOR_RGB(50, 113, 237).CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [_titleFollewView.layer addSublayer:gl];
    }
    return _titleFollewView;
}

-(UIImageView *)img{
    if (!_img) {
        _img = [UIImageView new];
        _img.image = [UIImage imageNamed:@"Mask group"];
    }
    return _img;
}

-(UILabel *)titileLabel{
    if (!_titileLabel) {
        _titileLabel = [UILabel new];
        _titileLabel.text = @"登录";
        _titileLabel.textColor = [UIColor whiteColor];
        _titileLabel.textAlignment = NSTextAlignmentCenter;
        _titileLabel.font = FONT18;
    }
    return _titileLabel;
}

-(UILabel *)msgLabel{
    if (!_msgLabel){
        _msgLabel = [UILabel new];
        _msgLabel.text = @"欢迎使用，";
        _msgLabel.font = FONT18;
        _msgLabel.textAlignment = NSTextAlignmentLeft;
        _msgLabel.textColor = [UIColor whiteColor];
    }
    return  _msgLabel;
}

-(UILabel *)msgTwoLabel{
    if (!_msgTwoLabel){
        _msgTwoLabel = [UILabel new];
        _msgTwoLabel.text = @"机场数字通行证系统";
        _msgTwoLabel.font = FONT16;
        _msgTwoLabel.textAlignment = NSTextAlignmentLeft;
        _msgTwoLabel.textColor = [UIColor whiteColor];
    }
    return  _msgTwoLabel;
}




-(UIView *)follewView{
    if (!_follewView) {
        _follewView = [UIView new];
        _follewView.backgroundColor = [UIColor whiteColor];
        _follewView.layer.cornerRadius = 18;
        _follewView.layer.masksToBounds = YES;
    }
    return _follewView;
}

-(UIImageView *)userNameImg{
    if (!_userNameImg) {
        _userNameImg = [UIImageView new];
        _userNameImg.image = [UIImage imageNamed:@"账号 1"];
    }
    return _userNameImg;
}

-(UILabel *)userNameLabel{
    if (!_userNameLabel) {
        _userNameLabel = [UILabel new];
        _userNameLabel.text = @"账号";
        _userNameLabel.font = FONT14;
        _userNameLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _userNameLabel;
}

-(UITextField *)userNameTextF{
    if (!_userNameTextF) {
        _userNameTextF = [UITextField new];
        _userNameTextF.placeholder = @"请输入账号";
        _userNameTextF.font = FONT14;
        _userNameTextF.textColor = [UIColor blackColor];
//        _userNameTextF.text = @"18911111111";
//        _userNameTextF.text = @"19600000000";
        _userNameTextF.text = @"admin";
    }
    return _userNameTextF;
}
-(UIView *)userNameV{
    if (!_userNameV) {
        _userNameV = [UIView new];
        _userNameV.backgroundColor = COLOR_RGB(217, 217, 217);
    }
    return _userNameV;;
}

-(UIImageView *)passwordImg{
    if (!_passwordImg) {
        _passwordImg = [UIImageView new];
        _passwordImg.image = [UIImage imageNamed:@"密码 1"];
    }
    return _passwordImg;
}

-(UILabel *)passwordLabel{
    if (!_passwordLabel) {
        _passwordLabel = [UILabel new];
        _passwordLabel.text = @"密码";
        _passwordLabel.font = FONT14;
        _passwordLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _passwordLabel;
}

-(UIButton *)forgetPassWordBtn{
    if (!_forgetPassWordBtn) {
        _forgetPassWordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetPassWordBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetPassWordBtn setTitleColor:COLOR_RGB(50, 113, 237) forState:UIControlStateNormal];
        _forgetPassWordBtn.titleLabel.font = FONT14;
        [_forgetPassWordBtn addTarget:self action:@selector(respondsTo_forgetPasswordBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetPassWordBtn;
}




-(UITextField *)passwordTextF{
    if (!_passwordTextF) {
        _passwordTextF = [UITextField new];
        _passwordTextF.placeholder = @"请输入登录密码";
        _passwordTextF.font = FONT14;
        _passwordTextF.textColor = [UIColor blackColor];
        _passwordTextF.secureTextEntry = YES;
//        _passwordTextF.text  =@"Epass@123456";
        
        // 添加显示/隐藏密码按钮
        UIButton *showHideButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [showHideButton setImage:[UIImage imageNamed:@"暗文"] forState:UIControlStateNormal];
        [showHideButton setImage:[UIImage imageNamed:@"明文"] forState:UIControlStateSelected];
        showHideButton.frame = CGRectMake(0, 0, 10, 10);
        [showHideButton addTarget:self action:@selector(togglePasswordVisibility:) forControlEvents:UIControlEventTouchUpInside];
        
        _passwordTextF.rightView = showHideButton;
        _passwordTextF.rightViewMode = UITextFieldViewModeAlways;
    }
    return _passwordTextF;
}
-(UIView *)passwordV{
    if (!_passwordV) {
        _passwordV = [UIView new];
        _passwordV.backgroundColor = COLOR_RGB(217, 217, 217);
    }
    return _passwordV;;
}

-(UIButton *)gxBtn{
    if (!_gxBtn){
        _gxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_gxBtn setImage:[UIImage imageNamed:@"未选中圆圈"] forState:(UIControlStateNormal)];
        [_gxBtn setImage:[UIImage imageNamed:@"勾"] forState:UIControlStateSelected];
        [_gxBtn addTarget:self action:@selector(respondsto_ChangeXieyiType:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _gxBtn;
}

-(UILabel *)xieyiLabel{
    if (!_xieyiLabel){
        _xieyiLabel = [UILabel new];
        _xieyiLabel.font = FONT12;
        _xieyiLabel.textAlignment = NSTextAlignmentLeft;
        _xieyiLabel.textColor = [UIColor blackColor];
        NSString * str =  @"我已经阅读并同意《用户协议》《隐私协议》";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        
        // 设置《用户协议》点击范围
        [attr addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(50, 113, 237)} range:NSMakeRange(8, 6)];
        // 设置《隐私协议》点击范围 
        [attr addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(50, 113, 237)} range:NSMakeRange(14, 6)];
        
        _xieyiLabel.attributedText = attr;
        _xieyiLabel.userInteractionEnabled = YES;
        
        // 添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnLabel:)];
        [_xieyiLabel addGestureRecognizer:tap];
    }
    return _xieyiLabel;
}

// 处理点击事件
- (void)handleTapOnLabel:(UITapGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:_xieyiLabel];
    
    // 获取点击位置所在的字符位置
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:_xieyiLabel.bounds.size];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:_xieyiLabel.attributedText];
    
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];
    
    textContainer.lineFragmentPadding = 0.0;
    
    NSUInteger characterIndex = [layoutManager characterIndexForPoint:point inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];
    
    // 判断点击的是哪个链接
    if (characterIndex >= 8 && characterIndex <= 13) {
        // 点击了用户协议
        NSLog(@"点击了用户协议");
        UserAgreementViewController * vc = [UserAgreementViewController new];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (characterIndex >= 14 && characterIndex <= 19) {
        // 点击了隐私协议
        NSLog(@"点击了隐私协议");
        UserPrivacyAgreementViewController * vc = [UserPrivacyAgreementViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}




-(UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn setTitle:@"下一步：输入短信验证码" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = FONT16;
        _nextBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        _nextBtn.layer.cornerRadius = 10;
        _nextBtn.layer.masksToBounds = YES;
        [_nextBtn addTarget:self action:@selector(respondsTo_pushNextViewController:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

-(HomeZJLoginPublicApiManager *)manager{
    if (!_manager) {
        _manager = [HomeZJLoginPublicApiManager new];
    }
    return _manager;
}



@end
